<template>
    <div class="register">
        <el-card style="width: 400px;height: 320px;text-align: center;background-color: rgba(255,255,255,0.8)">
            <h1 style="font-size: 18px;margin-bottom: 10px">博客注册</h1>

            <el-form size="medium" :model="form" :rules="rules" ref="formRef" label-width="70px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="form.nickname"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="form.password"></el-input>
                </el-form-item>
                <el-button style="width: 100%" @click="register('formRef')" type="success">注册</el-button>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Register",
        data() {
            return {
                form: {
                    username: "",
                    nickname: "",
                    password: ""
                },
                rules: {
                    username: [
                        {required: true, message: '用户名不能为空', trigger: 'blur'},

                    ], nickname: [
                        {required: true, message: '昵称不能为空', trigger: 'blur'},

                    ], password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'},

                    ],
                }
            }
        },
        methods: {
            register(formRef) {
                this.$refs[formRef].validate(async (valid) => { // 前端校验
                        if (valid) {
                            const {data: res} = await this.$http.post('user/register', this.form)
                            console.log(res);
                            if (res.meta.status === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '注册成功，可在首页自定义头像'
                                })
                                this.$router.push('login');
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: res.meta.msg
                                })
                            }

                        } else {
                            this.$message({
                                type: 'error',
                                message: '请检查！'
                            })
                            return false;
                        }
                    }
                );
            }
        }
    }
</script>

<style lang="less" scoped>
    .register {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100vh;
        background-color: #E9ECEF;
        background-size: 100% 100%;
        background-repeat: no-repeat;
            background-image: url("../assets/img10.jpg");
    }
</style>